﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Web;

namespace Web.SICOM.Models
{
    public class Imagen
    {
        public System.Drawing.Image ResizeImage(HttpPostedFileBase img, Size size)
        {
            var oImage = System.Drawing.Image.FromStream(img.InputStream);
            var sWidth = oImage.Width;
            var sHeight = oImage.Height;

            if (size.Width >= oImage.Width && size.Height >= oImage.Height)
                return (System.Drawing.Image)oImage;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sWidth);
            nPercentH = ((float)size.Height / (float)sHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int dWidth = (int)(sWidth * nPercent);
            int dHeight = (int)(sHeight * nPercent);

            Bitmap b = new Bitmap(dWidth, dHeight);
            Graphics g = Graphics.FromImage((System.Drawing.Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(oImage, 0, 0, dWidth, dHeight);
            g.Dispose();
            return (System.Drawing.Image)b;
        }


       public bool SaveImage(System.Drawing.Image img, string savePath)
        {
            var status = true;
            try
            {
                var newPath = new StringBuilder(savePath);
                // File.Delete(newPath.ToString());
                img.Save(newPath.ToString(), img.RawFormat);
            }
            catch (Exception)
            {
                status = false;
            }
            return status;
        }
    }
}